<template>
  <!-- 添加或修改合作伙伴对话框 -->
  <el-dialog :title="pd.title" v-model="pd.open" width="700px" append-to-body @close="closeAttachment">
    <el-form ref="partnerRef" :model="pd.form" :rules="pd.rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="pd.form.name" placeholder="请输入名称" />
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select v-model="pd.form.sex" placeholder="请选择性别">
              <el-option v-for="dict in hrp_user_sex" :key="dict.value" :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="社会编码" prop="idCard">
            <el-input v-model="pd.form.idCard" placeholder="请输入社会编码" />
          </el-form-item>
          <el-form-item label="诞生日期" prop="birthday">
            <el-date-picker clearable v-model="pd.form.birthday" type="date" value-format="YYYY-MM-DD"
              placeholder="请选择诞生日期">
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="pd.form.mobile" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item label="上级" prop="parentId">
            <el-input v-model="pd.form.parentId" placeholder="请输入上级" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="pd.form.email" placeholder="请输入邮箱" />
          </el-form-item>
          <el-form-item prop="picture">
            <Picture :idList="pd.form.picture" @resList="resList" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="6">
          <el-form-item label="是否启用" prop="enableFlag">
            <el-switch v-model="pd.form.enableFlag" active-value="0" inactive-value="1"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否客户" prop="isCustomer">
            <el-switch v-model="pd.form.isCustomer" active-value="0" inactive-value="1"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否供应商" prop="isSupplier">
            <el-switch v-model="pd.form.isSupplier" active-value="0" inactive-value="1"></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否公司" prop="isCompany">
            <el-switch v-model="pd.form.isCompany" active-value="0" inactive-value="1"></el-switch>
          </el-form-item>
        </el-col>

      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="pd.form.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="pd.open = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="PartnerCurd">
import { delPartner, addPartner, updatePartner, getPartner } from "@/api/hrp/partner";
import { getCurrentInstance, reactive } from "vue";
import Picture from "@/views/sys/attachment/Aattachment/picture";
const { proxy } = getCurrentInstance();
const { hrp_user_sex } = proxy.useDict('hrp_user_sex');
let pd = reactive({
  open: false,
  title: "",
  operation: 0,
  form: {
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    mobile: null,
    isSupplier: null,
    isCustomer: null,
    updateTime: null,
    params: []
  },
  rules: {
    enableFlag: [{
      required: true,
      message: "是否不能为空",
      trigger: "change"
    }],
    name: [{
      required: true,
      message: "名称不能为空",
      trigger: "blur"
    }],
    mobile: [{
      required: true,
      message: "手机号不能为空",
      trigger: "blur"
    }]
  }
});


function resList(list) {
  pd.form.picture = list
  console.log(list);
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["partnerRef"].validate(valid => {
    if (valid) {
      console.log(pd.form.picture);
      if (pd.operation == 1) {
        console.log(pd.form.picture);
        addPartner(pd.form).then(response => {
          proxy.$modal.msgSuccess("新增成功");
        });
      } else if (pd.operation == 2) {
        updatePartner(pd.form).then(response => {
          proxy.$modal.msgSuccess("修改成功");
        });
      }
      pd.open = false;
      emit("ok", pd.operation);
    }
  });
}

// 新增
function add(partner) {
  reset();
  //合并
  pd.form = { ...pd.form, ...partner };
  pd.title = "新增合作伙伴";
  pd.operation = 1;
  pd.open = true;
}
// update
function update(partner) {
  reset();
  //合并
  pd.form = { ...pd.form, ...partner };
  pd.title = "修改合作伙伴";
  pd.operation = 2;
  pd.open = true;
}
// del
function del(id) {
  remove()
  pd.operation = 3;
  proxy.$modal.confirm('是否确认删除合作伙伴编号为"' + ids + '"的数据项？').then(function () {
    return delPartner(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
    emit("ok", pd.operation);
  }).catch(() => {
  });
}
// 表单重置
function reset() {
  pd.form = {
    id: null,
    parentId: null,
    name: null,
    sex: null,
    mobile: null,
    email: null,
    idCard: null,
    birthday: null,
    enableFlag: "0",
    isSupplier: "1",
    isCustomer: "1",
    isCompany: "0",
    remark: null,
    params: {},
    picture: ''
  };
  pd.operation = 0;
  pd.title = "";
  proxy.resetForm("partnerRef");
}
defineExpose({ add, update, del });
const emit = defineEmits(["ok"]);

</script>
